<template>
  <section>
    <el-form label-width="80px">
      <el-form-item label="标题">
        <el-input size="mini" style="width: 100px" v-model="business.title"/>
      </el-form-item>
      <el-form-item label="编码">
        <el-input size="mini" style="width: 100px" v-model="business.code"/>
      </el-form-item>
      <el-form-item label="指标">
        <choice-indexs :business="business"/>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" @click="submitBtn(business)">{{business.id?'保存':'新建'}}</el-button>
      </el-form-item>
    </el-form>
  </section>
</template>

<script>
  import choiceIndexs from './choiceIndexs'
  import errHandler from '@/assets/errHandler.js'
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  export default {
    name: "editBusiness",
    props:['business'],
    components:{choiceIndexs},
    computed: {
      ...mapGetters(['department']),
    },
    created(){},
    methods:{
      ...mapActions({
        fetch: 'fetch'
      }),
      submitBtn(b){
        let data={
          title:b.title,code:b.code,data:b.data,tacticsIndexs:JSON.stringify(b.tacticsIndexs)
        }
        console.log('提交前的数据-----',data)
        // return
        if(b.id) {
          data.id=b.id
          this.updateDetectionBusiness(data)
        }
        else this.createDetectionBusiness(data)
      },
      updateDetectionBusiness(data){
        this.fetch({name:"updateDetectionBusiness",data})
          .then(res=>{
            console.log('更新业务-------',res)
            this.$message({type:'success',message:'更新成功！'})
            this.$emit('cancel')
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      createDetectionBusiness(data){
        this.fetch({name:"createDetectionBusiness",data})
          .then(res=>{
            console.log('创建业务-------',res)
            this.$message({type:'success',message:'创建成功！'})
            this.$emit('cancel')
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      }
    }
  }
</script>

<style scoped>

</style>
